package com.lewis.module.m2.api.luck.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lewis.common.core.entity.BaseResult;
import com.lewis.common.core.utils.DateUtil;
import com.lewis.module.m2.api.integral.entity.ImmortalIntegral;
import com.lewis.module.m2.api.integral.service.impl.ImmortalIntegralServiceImpl;
import com.lewis.module.m2.api.luck.mapper.LuckDayMapper;
import com.lewis.module.m2.api.luck.service.ILuckDayService;
import com.lewis.module.m2.api.luck.service.ILuckService;
import com.lewis.module.m2.api.luck.entity.Luck;
import com.lewis.module.m2.api.luck.entity.LuckDay;
import com.lewis.module.m2.api.luck.mapper.LuckMapper;
import lombok.Synchronized;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * <p>
 * 运势表 服务实现类
 * </p>
 *
 * @author lewis
 * @since 2021-12-01
 */
@Service
public class LuckServiceImpl extends ServiceImpl<LuckMapper, Luck> implements ILuckService {

	@Resource
	private LuckMapper luckMapper;

	@Resource
	private LuckDayMapper luckDayMapper;

	@Resource
	private ILuckDayService luckDayService;

	@Resource
	private ImmortalIntegralServiceImpl immortalIntegralService;

	@Override
	@Synchronized
	public BaseResult getLuck(String qq, String groupId) {
		List<String> list = new ArrayList<>();
		String sb = "";
		// 已存在签文
		BaseResult one = luckDayService.getOneByCtimeAndQq(new LuckDay().setQq(qq).setCtime(DateUtil.getDate()));
		if (one.getCode() == 200) {
			list.add("今日已经抽过签了。\n");
			sb = String.valueOf(one.getData());
		} else {
			// 扣除积分
			// 根据groupId和qq查到信息
			ImmortalIntegral immortalIntegral = immortalIntegralService.getOne(new LambdaQueryWrapper<ImmortalIntegral>().eq(ImmortalIntegral::getQq, qq).eq(ImmortalIntegral::getGroupId, groupId));
			if (immortalIntegral == null) {
				return BaseResult.fail().setMsg("不存在该仙友。");
			}
			int cut = 8;
			int lastNum = immortalIntegral.getIntegral() - cut;
			list.add("本次献祭仙力：" + cut + "\n");
			// 出现负数，禁止执行这个操作
			if (lastNum <= 0) {
				return BaseResult.fail().setMsg("仙力不足以执行本次操作");
			}
			immortalIntegral.setIntegral(lastNum);
			if (!immortalIntegralService.updateById(immortalIntegral)) {
				return BaseResult.fail().setMsg("抽签失败，请重试！");
			}

			// 随机获取一个运势
			Luck luck = this.getRandomOne();
			sb = "今日运势：" + luck.getLevel() + "\n" +
					"运势评级：" + luck.getStar() + "\n" +
					"运势签文：" + luck.getSimple() + "\n" +
					"运势解签：" + luck.getContent();
			// 获取成功后新增每日运势记录
			LuckDay luckDay = new LuckDay();
			luckDay.setLuckId(luck.getId());
			luckDay.setQq(qq);
			if (luckDayMapper.insert(luckDay) != 1) {
				// 创建记录失败则不返回
				return BaseResult.fail().setMsg("获取每日运势失败");
			}
		}

		list.add(sb);
		return BaseResult.ok().setData(list);
	}

	@Override
	public Luck getRandomOne() {
		List<Luck> list = luckMapper.selectList(Wrappers.emptyWrapper());
		return list.get(new Random().nextInt(list.size()));
	}
}
